function SDOrefreshData (force, cb) {
    // Get the date of the last version check. If it's more than one day old, check for new versions. Otherwise exit.
    var lastCheck = localStorage.getItem ("po_sdo_lastCheck");
    if (!lastCheck)
        lastCheck = 0;
    else
        lastCheck = Number (lastCheck);

   //localStorage.removeItem ('po_mfa_alerts');
    var today = new Date ();
    var todayCheck = Number(today.getTime());

    // Check for fresh data only every hour or if there's no stored info
    if (!force && todayCheck < (lastCheck+3600000) && localStorage.getItem ('po_sdo_alerts')) {
        return;
    }
    
    var url = POgetServerUrl ("alerter/po_sdo_feed.php", false);
    //var url = POgetServerUrl ("jirinatest/po_sdo_feed.php", false);
    var params = POgetServerParams ();
    var callback = function (httpRequest) {
//POlog ("po_sdo_menu: Response: ReadyState:"+httpRequest.readyState+" Status:"+httpRequest.status);
        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            var response = httpRequest.responseText;
            localStorage.setItem ('po_sdo_alerts', response);
            localStorage.removeItem ('po_sdo_alerts_local');
            localStorage.setItem ("po_sdo_lastCheck", String((new Date().getTime())));
//POlog ("po_sdo_menu: Data arrived:"+response);
            if (cb) {
                cb();
            }
        }
    }
        
    POajaxPost (url, "GET", params, callback);
    setTimeout(SDOrefreshData,300000);
//POlog ("po_sdo_menu: Data request sent. Url:"+url+" Params:"+params);
}
top.po_sdo_refresh = SDOrefreshData;

function SDOsendAlerts () {
    var queue = localStorage.getItem ("ffm_sightings_queue");
    localStorage.setItem ("ffm_sightings_queue", "");
    
    if (!queue || queue.length == 0) return;
    var processed = "";
    items = queue.split (";");
    var url = POgetServerUrl("alerter/savesighting.php", false);
    for (var i=0; i<items.length; i++) {
        var item = items[i];
        if (processed.indexOf (item) == -1) {
            processed += ";"+item;
            var data = item.split ("@");
            var query = "sname="+data[0]
            + "&ssector="+data[1]
            + "&scoords="+data[2]
            + "&universe="+POgetUniverse ()
            + "&userid="+POgetUserID ()
            + "&uname="+POgetUserName ()
            + "&vcode="+POgetValidationCode ();
            POajaxPost (url, "POST", query, null)
        }
    }
    setTimeout(SDOsendAlerts,300000);
}

// To ensure that the po_utils.js is already loaded
window.onload = setTimeout(function(){
   SDOrefreshData();
   setTimeout(SDOsendAlerts,300000);
}, 10);

